#!/usr/bin/perl -w
#
# $Id: ipv6_import.pl 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#       

use strict;
use warnings;
use ConfigSQL;
# use Netdot::Model;
use Getopt::Long;

Getopt::Long::Configure("default", "bundling");

push my @prog, split(/\//, $0);
my $program = $prog[$#prog];
my $arg = int(scalar(@ARGV));
my %opt = ();

sub main {
   GetOption(\%opt, "help|?", "conf|c=s") or usage();
   
   if ($opt{conf}) {
      my $database = undef;
      my $hostname = undef;
      my $username = undef;
      my $password = undef;

      my $count = 0;

      open(FH, $opt{conf}) or die "$program: cannot open $opt{conf}: $!";
      while (<FH>) {
         $count++;

         next if /^\s*#/;
         next if /^\s*$/;

         chomp(my $line = $_);
         if (line =~ /^\s*(db|host|user|pass)\s+(.*)\s*$/) {
            if ($1 eq "db") {
               chomp($database = $2);
            }
            elsif ($1 eq "host") {
               chomp($hostname = $2);
            }
            elsif ($1 eq "user") {
               chomp($username = $2);
            }
            elsif ($1 eq "pass") {
               chomp($password = $2);
            }
            else {
               close(FH);
               die "$program: undefined token $1 in line $count";
            }
         }
      }

      close(FH);

      my $sql = new ConfigSQL($database, $hostname, $username, $password);
      my $sqlQuery = "select * from IPv6_Prefix;";
      my @temp = $sql->execute($sqlQuery);

      foreach (@temp) {
         my ($id, $block, $purpose) = $_;
         my ($address, $prefix) = split(/\//, $block);
         die "$program: Block must be in CIDR notation: $block\n" unless(
            $address and $prefix);
         my $ent = Entity->find_or_create(name => "Network Services");
         my $ent2 = Entity->find_or_create(name => "Network Engineering");
         my $status = "Container";

         my $ip = Ipblock->search(address => $address, prefix=> $prefix)->first;
         if ($ip) {
            printf "IP " . $ip->get_label . " exists. Updating.\n";
            $ip->update({ 
                  status   => $status,
                  owner    => $ent,
                  used_by  => $ent2,
                  description => $purpose,
               });
         }
         else {
            my $newip;
            eval {
               $newip = Ipblock->insert({
                     address     => $address,
                     prefix      => $prefix,
                     owner       => $ent,
                     used_by     => $ent2,
                     description => $purpose,
                     no_update_tree => 1,
                  });
            };

            if (my $error = $@) {
               die "ERROR: $error\n";
            }
            else {
               print "Inserted " . $newip->get_label . "\n";
            }
         }

         Ipblock->build_tree(4);
         Ipblock->build_tree(6);
      }

      $sql->close();
   }
}

if ($arg le 1) {
   usage();
}
else {
   main();
}


